package com.ysz.timewheel.server.task;

import com.ysz.timewheel.Starter;
import com.ysz.timewheel.TimeWheel;
import com.ysz.timewheel.server.struct.ClientStruct;
import lombok.SneakyThrows;
import org.apache.commons.lang3.RandomUtils;


/**
 * @ClassName DeactivationTask
 * @Description TODO
 * @Data 10:48
 * @Version 1.0
 * @Author ysz
 */
public class DeactivationTask implements Runnable {

    /**
     * 模拟失活ip  就不去数据库查询了
     */
    private static volatile String ip;

    @SneakyThrows
    @Override
    public void run() {
        try {
            //启动模拟线程
            new Thread(new SimulationDeactivation()).start();
            while (true) {
                //三秒扫描一次数据库
                Thread.sleep(2000);
                //构建clientStruct
                ClientStruct clientStruct = new ClientStruct(ip, null);
                //从失活map移除
                TimeWheel.getDeactivationClientsTime().remove(clientStruct);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static class SimulationDeactivation implements Runnable {

        @SneakyThrows
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(3000);
                    DeactivationTask.ip = Starter.deactivationNode[RandomUtils.nextInt(0, Starter.deactivationNode.length)];
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
